/*
 * Java Basics Practical Tasks module1
 * 3. Anagram task
 */

package module1;

import java.util.*;

/**
 *
 * @author Antonina Cherednichenko
 */
public class Anagram {
    /*
     * Deleting whitespaces, arranging case
     */
    private static char[] prepareForCheck(String oldString) {
       String newString = oldString.replaceAll("\\s","").toLowerCase();
       return newString.toCharArray();
    }

    /**
     * Function convers strings into char arrays and comparing their equality
     */
    private static boolean areAnagrams(String firstSentence, String secondSentence) {
        char[] ch1 = prepareForCheck(firstSentence);
        char[] ch2 = prepareForCheck(secondSentence);
        Arrays.sort(ch1);
        Arrays.sort(ch2);
        return Arrays.equals(ch1,ch2);
    }

    /*
     * Function return useful for printing answer format. In this case - yes/no format
     */
    private static String getFormattedAnswer(boolean answer) {
        if (answer) {
            return "Yes";
        }
        return "No";
    }

    /**
     * Function for linking all logic together, is called from main Module1 class
     */
    public static void testFunction() {
        Scanner in = new Scanner(System.in);
        System.out.println("Testing function for Anagram quest");
        System.out.println("Enter first string");
        String firstSentence = in.nextLine();
        System.out.println("Enter second string");
        String secondSentence = in.nextLine();
        String answer = getFormattedAnswer(areAnagrams(firstSentence, secondSentence));
        System.out.printf("Are '%s' and '%s' anagrams? Answer: %s\n", firstSentence, secondSentence, answer);
    }
}
